package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kun.index.entity.Blog;
import com.kun.index.entity.BlogLabel;
import com.kun.index.entity.Label;
import com.kun.index.mapper.BlogLabelMapper;
import com.kun.index.mapper.LabelMapper;
import com.kun.index.service.LabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-18
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {

    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private BlogLabelMapper blogLabelMapper;

    @Override
    public List<Label> queryLabels(HashMap<String, Object> map) {
        QueryWrapper<Label> wrapper = new QueryWrapper<>();
        if (map.get("id")!=null){
            Integer id= (Integer) map.get("id");
            wrapper.eq("id",id);
        }
        return labelMapper.selectList(wrapper);

    }

    @Override
    public Integer addLabel(Label label) {
        labelMapper.insert(label);
        return  label.getId();

    }

    @Override
    public int batchDeleteLabel(List<Integer> ids) {
        return labelMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteLabel(Label label) {
        return labelMapper.deleteById(label.getId());
    }

    @Override
    public List<Label> queryByBlogId(Integer blogId) {
        QueryWrapper<BlogLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",blogId);
        List<BlogLabel> blogLabels = blogLabelMapper.selectList(wrapper);
        ArrayList<Label> list = new ArrayList<>();
        for (BlogLabel blogLabel : blogLabels) {
            list.add(labelMapper.selectById(blogLabel.getLabelId()));
        }

        return list;
    }

    @Override
    public Integer updateLabel(Label label) {
        return labelMapper.updateById(label);
    }

    @Override
    public Label queryByLabelId(Integer queryLabelId) {
        return labelMapper.selectById(queryLabelId);
    }

    @Override
    public List<Label> queryByLabelIds(ArrayList<Integer> labelIds) {
        ArrayList<Label> list = new ArrayList<>();
        for (Integer labelId : labelIds) {
            Label label = this.queryByLabelId(labelId);
            list.add(label);
        }
        return list;
    }
}
